# Maintainer : J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=chardet
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-chardet" "${MINGW_PACKAGE_PREFIX}-python2-chardet")
pkgver=3.0.4
pkgrel=1
arch=('any')
url="https://github.com/chardet/chardet"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools" "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
#More comprehensive than PyPi source
source=("${_realname}-${pkgver}.tar.gz::https://github.com/chardet/chardet/archive/${pkgver}.tar.gz")
sha256sums=('d5620025cfca430f6c2e28ddbc87c3c66a5c82fa65570ae975c92911c2190189')

prepare() {
  cd "${srcdir}"
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
  done
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.  
build() {  
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done
}

check() {  
  for pver in {2,3}; do  
    msg "Python ${pver} test for ${CARCH}"  
    cd "${srcdir}/python2-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} test.py
  done
}

package_python3-chardet() {
   depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
   pkgdesc="Python3 module for character encoding auto-detection (mingw-w64)"

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

package_python2-chardet() {
   depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
   pkgdesc="Python2 module for character encoding auto-detection (mingw-w64)"

   local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

   cd "${srcdir}/python2-build-${CARCH}"
   MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
   ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done

  for f in chardetect; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"

}

package_mingw-w64-i686-python2-chardet() {
  package_python2-chardet
}

package_mingw-w64-i686-python3-chardet() {
  package_python3-chardet
}

package_mingw-w64-x86_64-python2-chardet() {
  package_python2-chardet
}

package_mingw-w64-x86_64-python3-chardet() {
  package_python3-chardet
}
